package com.os.ordersystem.controller;

import com.os.ordersystem.bean.*;
import com.os.ordersystem.service.OrderItemService;
import com.os.ordersystem.service.OrderService;
import com.os.ordersystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private UserService userService;

    //订单新增
    @PostMapping("add")
    public ResultDTO add(@RequestBody List<Food> orders,
                         @RequestParam Integer id,
                         @RequestParam String orderRemarks,
                         @RequestParam Double sum,
                         @RequestParam Integer orderType,
                         @RequestParam String presetTime,
                         @RequestParam Integer opera) throws ParseException {

        Order order = new Order();
        order.setUserId(id);
        order.setSum(sum);
        order.setOrderRemarks(orderRemarks);
        order.setOrderType(orderType);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = sdf.parse(presetTime);
        order.setPresetTime(date);
        if (opera == 0) {
            order.setStatus(0);
        }else {
            order.setStatus(1);
        }

        order.setCreateTime(new Date());
        int os = orderService.add(order);
        //添加用户积分
        int us = userService.insertIntegral(id,sum);
        for (int i = 0; i < orders.size(); i++) {
            if (orders.get(i).getNum() != 0) {
                OrderItem orderItem = new OrderItem();
                orderItem.setOrderId(os);
                orderItem.setFoodId(orders.get(i).getId());
                orderItem.setFoodCount(orders.get(i).getNum());
                orderItemService.addOrderItem(orderItem);
            }
        }
        return ResultDTO.success();
    }

    //商家查看所有订单
    @GetMapping("/getByPage")
    public ResultDTO getByPage(@RequestParam String name,
                                @RequestParam Integer page,@RequestParam Integer size) {

        Map<String, Object> params = new HashMap<>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        params.put("name", name);

        List<Order> byPage = orderService.getByPage(params);

        Page<Order> orderPage = new Page<>();

        long total = orderService.getTotal(name);
        orderPage.setRows(byPage);
        orderPage.setTotal(total);
        orderPage.setPageNum(page);
        orderPage.setPageSize(size);

        return ResultDTO.success(orderPage);
    }

    //用户查看个人订单
    @GetMapping("/getByPageC")
    public ResultDTO getByPageC(@RequestParam Integer id,
                               @RequestParam Integer page,@RequestParam Integer size) {

        Map<String, Object> params = new HashMap<>();
        params.put("page", (page-1)*size);
        params.put("size", size);
        params.put("id", id);

        List<Order> byPage = orderService.getByPageC(params);

        Page<Order> orderPage = new Page<>();

        long total = orderService.getTotalC(id);
        orderPage.setRows(byPage);
        orderPage.setTotal(total);
        orderPage.setPageNum(page);
        orderPage.setPageSize(size);

        return ResultDTO.success(orderPage);
    }

    //模拟付款
    @PostMapping("/payOrder")
    public ResultDTO payOrder(@RequestParam Integer id) {

        int i = orderService.payOrder(id);

        return ResultDTO.success(1);
    }

    //删除订单
    @DeleteMapping("/del")
    public ResultDTO del(@RequestParam Integer id) {

        int i = orderService.del(id);
        int j = orderItemService.del(id);

        return ResultDTO.success();
    }



}
